package com.book.modules.exception;

import com.book.common.exception.BaseException;
import com.book.common.helper.I18nHelper;
import com.book.common.result.Failed;
import com.book.common.result.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Description： 全局异常处理
 *
 * @Author： leo.xiong
 * @CreateDate： 2019/10/19 15:01
 * @Email： leo.xiong@suyun360.com
 * Version：1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @Autowired
    private I18nHelper i18nHelper;

    /**
     * 全局异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public RestResponse exceptionHandler(Exception e) {
        return new Failed("BOOK:COMMON:10029", i18nHelper.get("BOOK:COMMON:10029") + e.getMessage());
    }

    /**
     * 全局运行异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = RuntimeException.class)
    public RestResponse runtimeExceptionHandler(RuntimeException e) {
        return new Failed("BOOK:COMMON:10031", i18nHelper.get("BOOK:COMMON:10031") + e.getMessage());
    }

    /**
     * 自定义异常信息
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BaseException.class)
    public RestResponse exceptionHandler(BaseException e) {
        return new Failed("BOOK:COMMON:10032", i18nHelper.get("BOOK:COMMON:10032") + e.getMessage());
    }

    /**
     * 用户信息异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = CurrentUserException.class)
    public RestResponse currentUserExceptionHandler(CurrentUserException e) {
        return new Failed("BOOK:COMMON:10033", i18nHelper.get("BOOK:COMMON:10033") + e.getMessage());
    }

    /**
     * 限流异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = LimitAccessException.class)
    public RestResponse limitAccessExceptionHandler(LimitAccessException e) {
        return new Failed("BOOK:COMMON:10034", i18nHelper.get("BOOK:COMMON:10034") + e.getMessage());
    }

    /**
     * 授权异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = OauthException.class)
    public RestResponse oauthExceptionHandler(OauthException e) {
        return new Failed("BOOK:COMMON:10035", i18nHelper.get("BOOK:COMMON:10035") + e.getMessage());
    }

    /**
     * redis异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = RedisConnectException.class)
    public RestResponse redisConnectExceptionHandler(RedisConnectException e) {
        return new Failed("BOOK:COMMON:10036", i18nHelper.get("BOOK:COMMON:10036") + e.getMessage());
    }
}
